import router from "./router";
import nProgress from "nprogress";
import 'nprogress/nprogress.css';
import store from '@/store'; //获取不到status


const whiteList = ['/login','/404']
// 前置守卫
router.beforeEach((to,from,next)=>{
  nProgress.start()
  if(store.getters.token){
    
    //跳转到主页
    if(to.path === '/login'){
      next('/')
      nProgress.done()
    }else{

      if(!store.getters.userId){
        store.dispatch('user/getUserInfo')
      }
      next()
    }
  }else{
    //没有token
    if(whiteList.includes(to.path)){
      next()
    }else{
      next('/login')
      nProgress.done()
    }
  }
})


//后置守卫
router.afterEach(()=>{
  nProgress.done()
})